﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ElingOA6_Admin_DSJCJ.BLL
{
    /// <summary>
    /// Item业务逻辑层
    /// </summary>
    public partial class Item
    {
        private readonly DAL.Item dal = new DAL.Item();
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        /// <param name="itemClsNo">类别编号</param>
        /// <returns>是否存在</returns>
        public bool ExistsCls(string itemClsNo)
        {
            return dal.ExistsCls(itemClsNo);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public List<ElingOA6_Admin_DSJCJ.Model.Item> GetModelList(string message)
        {
            DataSet ds = dal.GetList(message);
            return DataTableToList(ds.Tables[0]);
        }


        /// <summary>
        ///  获得数据列表
        /// <summary>
        /// <creator>chenly</creator>
        /// <date>2013-9-29</date>
        /// </summary>
        /// <param name="dt">一张DataTable</param>
        /// <returns>返回Model</returns>
        public List<ElingOA6_Admin_DSJCJ.Model.Item> DataTableToList(DataTable dt)
        {
            List<ElingOA6_Admin_DSJCJ.Model.Item> modelList = new List<ElingOA6_Admin_DSJCJ.Model.Item>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                ElingOA6_Admin_DSJCJ.Model.Item model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = dal.DataRowToModel(dt.Rows[n]);
                    if (model != null)
                    {
                        modelList.Add(model);
                    }
                }
            }
            return modelList;
        }
        /// <summary>
        ///  获得数据列表
        /// <summary>
        /// <creator>chenly</creator>
        /// <date>2013-9-23</date>
        /// </summary>
        /// <param name="dt">一张DataTable</param>
        /// <returns>返回Model</returns>
        public List<Model.Item> DataTableToListItem(DataTable dt)
        {
            List<Model.Item> modelList = new List<Model.Item>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Model.Item model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Model.Item();
                    if (dt.Rows[n]["FItemNo"] != null)
                    {
                        model.FItemNo = dt.Rows[n]["FItemNo"].ToString();
                    }
                    if (dt.Rows[n]["FIsUse"] != null)
                    {
                        model.FIsUse = dt.Rows[n]["FIsUse"].ToString();
                    }
                    if (dt.Rows[n]["FSafetyStock"] != null && dt.Rows[n]["FSafetyStock"].ToString() != "")
                    {
                        model.FSafetyStock = decimal.Parse(dt.Rows[n]["FSafetyStock"].ToString());
                    }
                    if (dt.Rows[n]["FEntityNo"] != null)
                    {
                        model.FEntityNo = dt.Rows[n]["FEntityNo"].ToString();
                    }
                    if (dt.Rows[n]["FItemClsNo"] != null)
                    {
                        model.FItemClsNo = dt.Rows[n]["FItemClsNo"].ToString();
                    }
                    if (dt.Rows[n]["FItemClsName"] != null)
                    {
                        model.FItemClsName = dt.Rows[n]["FItemClsName"].ToString();
                    }
                    if (dt.Rows[n]["FItemName"] != null)
                    {
                        model.FItemName = dt.Rows[n]["FItemName"].ToString();
                    }
                    if (dt.Rows[n]["FItemSpec"] != null)
                    {
                        model.FItemSpec = dt.Rows[n]["FItemSpec"].ToString();
                    }
                    if (dt.Rows[n]["FUnit"] != null)
                    {
                        model.FUnit = dt.Rows[n]["FUnit"].ToString();
                    }
                    if (dt.Rows[n]["FItemClsName"] != null)
                    {
                        model.FItemClsName = dt.Rows[n]["FItemClsName"].ToString();
                    }
                    if (dt.Rows[n]["FPrice"] != null && (dt.Rows[n]["FPrice"].ToString() != ""))
                    {
                        model.FPrice = decimal.Parse(dt.Rows[n]["FPrice"].ToString());
                    }
                    if (dt.Rows[n]["FQty"] != null && (dt.Rows[n]["FQty"].ToString() != ""))
                    {
                        model.FQty = decimal.Parse(dt.Rows[n]["FQty"].ToString());
                    }
                    if (dt.Rows[n]["FAmount"] != null && (dt.Rows[n]["FAmount"].ToString() != ""))
                    {
                        model.FAmount = decimal.Parse(dt.Rows[n]["FAmount"].ToString());
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        /// <param name="model">model对象</param>
        /// <returns>是否增加</returns>
        public void Add(ElingOA6_Admin_DSJCJ.Model.Item itemModel, Model.ItemYearBal itemYearBalModel)
        {
            dal.Add(itemModel, itemYearBalModel);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        /// <param name="model">model对象</param>
        /// <returns>是否更新</returns>
        public bool Update(ElingOA6_Admin_DSJCJ.Model.Item model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<ElingOA6_Admin_DSJCJ.Model.Item> GetListAll(string strWhere)
        {
            DataSet ds = dal.GetListAll(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
      
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        /// <param name="itemNo">用品编号</param>
        /// <param name="entityNo">公司编号</param>
        /// <returns>实体对象</returns>
        public ElingOA6_Admin_DSJCJ.Model.Item GetModel(string itemNo, string entityNo)
        {
            return dal.GetModel(itemNo, entityNo);
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        /// <param name="itemNo">用品编号</param>
        /// <param name="entityNo">公司编号</param>
        /// <returns>是否存在该记录</returns>
        public bool Exists(string itemNo, string entityNo)
        {
            return dal.Exists(itemNo, entityNo);
        }
        /// <summary>
        /// 批量删除数据 
        /// <creator>chenly</creator>
        /// <date>2013-9-23</date>
        /// </summary>
        /// <param name="itemArray">编号数组</param>
        /// <param name="entityNo">公司编号</param>
        public void DelManyItem(string[] itemArray, string entityNo, string year)
        {
            dal.DelManyItem(itemArray, entityNo, year);
        }
        /// <summary>
        /// 删除数据 
        /// <creator>chenly</creator>
        /// <date>2013-9-23</date>
        /// </summary>
        /// <param name="itemNo">编号</param>
        /// <param name="entityNo">公司编号</param>
        public void Delete(string itemNo, string entityNo, string year)
        {
            dal.Delete(itemNo, entityNo, year);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="itemName"></param>
        /// <returns></returns>
        public string GetItemNoByName(string itemName)
        {
            return dal.GetItemNoByName(itemName);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="itemName"></param>
        /// <returns></returns>
        public string GetItemNo(string itemNo)
        {
            return dal.GetItemNo(itemNo);
        }

    }
}
